<template>
  <div>
    <div class="app-container">
      <el-form :model="countQueryParams" ref="countQueryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="自定义时间段" label-width="96px" prop="params">
          <el-date-picker
            v-model="countDateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleCountQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetCountQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="app-log-statics-wrapper">
        <div v-for="item in staticsList" class="statics-item">
          <div class="statics-item-title">{{ item.applyName }}（次）</div>
          <div class="statics-item-count">{{ item.statisticsLog }}</div>
        </div>
      </div>
    </div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="应用名称" prop="logtype">
          <el-select style="width: 100%" v-model="queryParams.logtype" placeholder="请选择">
            <el-option v-for="dict in staticsList" :key="dict.id" :label="dict.applyName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自定义时间段" label-width="100px" prop="params">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="tableList">
        <el-table-column label="工号/学号" align="center" prop="studentId" />
        <el-table-column label="姓名" align="center" prop="userName" />
        <el-table-column label="时间" align="center" prop="loginTime" />
        <el-table-column label="应用名称" align="center" prop="applyName" />
        <el-table-column label="IP" align="center" prop="ipaddr" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <span>{{ scope.row.status === '0' ? '成功' : '失败' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="详情" align="center" prop="msg" />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
  import { getAppAuthList } from '../appAuth/service'
  import { getAppLogStatics } from './service'
  export default {
    name: 'AppLog',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        dateRange: [],
        countDateRange: [],
        typeOptions: [],
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        tableList: [],
        staticsList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          logtype: '',
          params: {}
        },
        // 查询参数
        countQueryParams: {
          params: {}
        }
      }
    },
    created() {
      this.getCount()
      this.getList()
    },
    methods: {
      /* 获取 统计 数据 */
      getCount() {
        getAppLogStatics(this.countQueryParams).then(response => {
          if (response.code === 200) {
            this.staticsList = response.data || []
          }
        })
      },

      /** 查询列表 */
      getList() {
        this.loading = true
        getAppAuthList(this.queryParams)
          .then(response => {
            this.tableList = response.rows
            this.total = response.total
          })
          .finally(() => {
            this.loading = false
          })
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        if (this.dateRange && this.dateRange[0]) {
          this.queryParams.params.beginTime = this.dateRange[0]
          this.queryParams.params.endTime = this.dateRange[1]
        } else {
          this.queryParams.params = {}
        }
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.queryParams.params = {}
        this.dateRange = []
        this.handleQuery()
      },

      /** 搜索按钮操作 */
      handleCountQuery() {
        if (this.countDateRange && this.countDateRange[0]) {
          this.countQueryParams.params.beginTime = this.countDateRange[0]
          this.countQueryParams.params.endTime = this.countDateRange[1]
        } else {
          this.countQueryParams.params = {}
        }
        this.getCount()
      },
      /** 重置按钮操作 */
      resetCountQuery() {
        this.resetForm('countQueryForm')
        this.countQueryParams.params = {}
        this.countDateRange = []
        this.handleCountQuery()
      }
    }
  }
</script>
<style lang="scss" type="text/scss">
  .app-log-statics-wrapper {
    display: flex;
    align-items: center;
    margin: 2px 0 12px;
    border: 1px solid #dfe6ec;
    border-bottom: none;
    border-radius: 4px;
    flex-wrap: wrap;
    .statics-item {
      width: 20%;
      box-sizing: border-box;
      border-right: 1px solid #dfe6ec;
      border-bottom: 1px solid #dfe6ec;
      display: flex;
      padding: 20px 0 16px;
      flex-direction: column;
      align-items: center;
      .statics-item-title {
        font-size: 16px;
        font-weight: bold;
      }
      .statics-item-count {
        margin-top: 12px;
        font-size: 18px;
        font-weight: bold;
      }
    }
  }
</style>
